package com.Zhengxin.smarthome.service;

import com.Zhengxin.smarthome.ConnectService;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.IBinder;

public class AutoLight extends Service implements SensorEventListener{
	
	SensorManager  mySensorManager;
	Sensor lightSensor = null;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		System.out.println("service start");
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mySensorManager.unregisterListener(this);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		mySensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
		lightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
		mySensorManager.registerListener(this,lightSensor, SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		float[] values = event.values;
		System.out.println("values"+values[0]);
		if(values[0]<50){
			Intent intent= new Intent(AutoLight.this,ConnectService.class);
			Bundle bundle= new Bundle();
			bundle.putString("config","Curtain_CONTROL_On");
			intent.putExtras(bundle);
			startService(intent);
		}
		if(values[0]>=50){
			Intent intent= new Intent(AutoLight.this,ConnectService.class);
			Bundle bundle= new Bundle();
			bundle.putString("config","Curtain_CONTROL_Off");
			intent.putExtras(bundle);
			startService(intent);
		}
	}

}
